﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;

namespace teach
{
    public partial class VideoPanel : PanelBase
    {
        private string m_videoPath = null;

        public VideoPanel()
        {
            InitializeComponent();
        }

        private float GetFont(string data)
        {
            float size = 36;
            if (data.Length > 100)
            {
                size = 20;
            }
            else if (data.Length > 75
                )
            {
                size = 22;
            }
            else if (data.Length > 45)
            {
                size = 24;
            }
            else if (data.Length > 20)
            {
                size = 32;
            }
            return size;
        }

        /// <summary>
        /// Set the operation question running
        /// </summary>
        /// <param name="question"></param>
        public override void SetQuestion(Question question)
        {
            Reset();
            m_answered = false;
            m_currentQuestion = question;
            txtAnswer.Text = "";
            txtAnswer.Font = new System.Drawing.Font("Microsoft Sans Serif", GetFont(question.Answer.ToString()), System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

            m_videoPath = Path.Combine(m_additionalResourcePath, question.Properties["video"]);

            this.timeoutMeter.Interval = question.Timeout;
            this.timeoutMeter.Enabled = true;
        }

        public override Control GetAnswerControl()
        {
            return txtAnswer;
        }

        public override void SetStatusImage(bool success, Image image)
        {
            pctStatus.Image = image;
            pctStatus.Refresh();
            txtAnswer.ReadOnly = true;
            if (success)
            {
                txtAnswer.BackColor = Color.LightBlue;
            }
            else
            {
                txtUserAnswer.Visible = true;
                txtAnswer.BackColor = Color.Red;
                txtAnswer.Text = m_currentQuestion.Answer.ToString();
            }
        }

        private void Reset()
        {
            pctStatus.Image = null;
            pctStatus.Refresh();
            txtAnswer.ReadOnly = false;
            txtAnswer.ForeColor = Color.Black;
            txtAnswer.BackColor = Color.White;
            txtAnswer.Text = "";
            txtUserAnswer.Visible = false;
            btnNext.Text = "RÉPONSE";
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            m_answered = !m_answered;
            this.timeoutMeter.Enabled = false;
            if (m_answered)
            {
                if (txtAnswer.Text.Trim() == string.Empty)
                {
                    m_answered = !m_answered;
                    return;
                }
                txtUserAnswer.Text = "Vous aviez répondu:" + Environment.NewLine + Environment.NewLine + txtAnswer.Text;
                base.RaiseAnswer(txtAnswer.Text);
                btnNext.Text = "Prochain";
            }
            else
            {
                player.Ctlcontrols.stop();
                Reset();
                base.RaiseNextClicked();
            }
        }

        private void btnAudio_Click(object sender, EventArgs e)
        {
            //SoundPlayer simpleSound = new SoundPlayer(m_audioPath);
            //simpleSound.Play();

            player.URL = m_videoPath;
            player.stretchToFit = true;
            player.Ctlcontrols.play();
            txtAnswer.Focus();
        }
    }
}
